<?php

function arrayValuesTrim(&$value, $key) {
    $value = trim($value);
}

/**
 * search actions.
 *
 * @package    travide
 * @subpackage search
 * @author     Travide Project Group
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchActions extends sfActions {

    /**
     * Executes a query string - and returns search results
     *
     * Expects request parameter 'queryString' to provide the search request
     *
     * If query string is empty then returns null object
     *
     * If query returns no results - then an empty Doctrine_Collection is
     * returned (indicating an empty result set)
     *
     * @param sfWebRequest $request
     */
    public function executeQuery(sfWebRequest $request) {

        $this->searchResults = null;
        $this->queryTypeLimiterName = null;
        $this->searchError = '';
        $this->queryString = $request->getParameter('queryString');
        $this->queryTypeLimiter = $request->getParameter('queryTypeLimiter');
        if (strlen($this->queryTypeLimiter) == 0) {
            $this->queryTypeLimiter = null;
        } else {
            $collectionType = Doctrine::getTable('CollectionType')->findOneBy('uin', $this->queryTypeLimiter);
            if ($collectionType != null) {
                $this->queryTypeLimiterName = $collectionType->getTypeName();
            } 
        }
        $this->getUser()->setAttribute('lastQueryString', $this->queryString);
        $this->getUser()->setAttribute('lastQueryTypeLimiter', $this->queryTypeLimiter);
        try {
            if (strlen($this->queryString) > 0) {
                $query = Doctrine::getTable('CollectionItem')->getSearchQuery($this->queryString, $this->queryTypeLimiter);
                if ($query != null) {
                    $this->searchResults = $query->execute();
                } else {
                    $this->searchResults = new Doctrine_Collection('CollectionItem');
                }
            }
        } catch (Exception $e) {
            $this->searchError = $e->getMessage();
        }

        /**
         * Supply the page with a listing of Collection Types
         */
        $this->collectionTypes = Doctrine::getTable('CollectionType')->findAll();
    }

    public function executeOpenSearchDescriptor(sfWebRequest $request) {
        
    }

    public function executeTagBrowse(sfWebRequest $request) {

        $this->searchResults = null;
        $this->searchTags = null;
        $this->relatedTags = null;
        $this->allTags = null;

        $this->tagsString = $request->getParameter('tags');
        if (strlen($this->tagsString) > 0) {
            $this->searchTags = explode(',', $this->tagsString);
            array_walk($this->searchTags, 'arrayValuesTrim');
            $this->tagString = implode(',', $this->searchTags);

            $query = Doctrine::getTable('CollectionItem')->getCollectionsByTagNamesQuery($this->searchTags);
            $this->searchResults = $query->execute();
            if (sfConfig::get('app_features_enable_related_tags')) {
                $relatedQuery = Doctrine::getTable('SubjectTag')->getRelatedSubjectTagsQuery($this->searchTags);
                $this->relatedTags = $relatedQuery->execute();
            }
        } else {
            $q = Doctrine::getTable('SubjectTag')->addOrderByFrequencyQueryDesc();
            $this->allTags = $q->execute();
        }
    }

}
